package com.example.base_kotlin_xml.view

import android.os.Bundle
import com.blankj.utilcode.util.LogUtils
import com.example.base_kotlin_xml.MainActivity
import com.example.base_kotlin_xml.R
import com.example.base_kotlin_xml.databinding.FragmentMoshiBinding
import com.example.base_model.bean.general.TestRequest
import com.example.base_model.ext.fromJson
import com.example.base_model.ext.toJson
import com.example.base_model.view.BaseFragment
import com.squareup.moshi.Moshi
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

/**
 * @Author: jiangKunKun
 * @CreateDate: 2025/7/30
 * @Description: moshi 解析
 * @SpittingGrooves
 */
@AndroidEntryPoint
class MoshiFragment : BaseFragment<FragmentMoshiBinding>() {
    private var testRequest: TestRequest? = null
    private var testMoshiJson: String = "{\"name\":\"jkk\",\"password\":\"123456789\",\"code\":\"101\",\"uuid\":\"789789789\"}"
    @Inject
    lateinit var moshi: Moshi
    override fun initObservers() {
        
    }

    override fun initClickEvents() {
        mBinding.btnMoshiToJson.setOnClickListener {
            val toJson = moshi.toJson(testRequest, indent = false)
            LogUtils.d("toJson", toJson)
        }
        mBinding.btnMoshiFromJson.setOnClickListener {
            val fromJson = moshi.fromJson<TestRequest>(testMoshiJson)
            val fromJson1 = moshi.fromJson(testMoshiJson, TestRequest::class.java)
            LogUtils.d("fromJson", fromJson)
            LogUtils.d("fromJson1", fromJson1)
        }
        // 去登录
        mBinding.btnGoLogin1.setOnClickListener {
            (activity as? MainActivity)?.navigateToLoginAndClearStack(true)
        }
        mBinding.btnGoLogin2.setOnClickListener {
            navigate(R.id.action_moshiFragment_to_loginFragment, createLoginArgs(false))
        }
    }

    /**
     * 创建登录参数Bundle
     */
    private fun createLoginArgs(isForceLogin: Boolean): Bundle {
        return Bundle().apply {
            putBoolean("isForceLogin", isForceLogin)
        }
    }


    override fun initData() {
        testRequest = TestRequest("jkk", "456", "101", "999999999")
    }

    override fun attachLayoutRes(): Int = R.layout.fragment_moshi
}